package com.shanjupay.operationapp.controller;

import com.shanjupay.common.domain.RestResponse;
import com.shanjupay.common.util.EncryptUtil;
import com.shanjupay.operationapp.convert.AccountCovert;
import com.shanjupay.operationapp.vo.AccountVO;
import com.shanjupay.user.api.AccountService;
import com.shanjupay.user.api.dto.AccountDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/m/users")
@Api(value = "用户管理", tags = "User", description="包括租户内用户、角色、权限自管理")
public class UserController {


    @Reference
    private AccountService accountService;


    @ApiOperation("当前租户下创建账号")
    @ApiImplicitParam(name = "account", value = "账号信息", required = true, dataType = "AccountVO", paramType = "body")
    @PostMapping("/accounts")
    public  RestResponse<AccountVO> createAccount(@RequestBody AccountVO account) {
        AccountDTO rst = accountService.createAccount(AccountCovert.INSTANCE.vo2dto(account));
        return RestResponse.success(AccountCovert.INSTANCE.dto2vo(rst));
    }


    @ApiOperation("通过id获取当前租户下某账号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jsonToken", value = "用户及接入应用信息", dataType = "String", paramType="query"),
            @ApiImplicitParam(name = "id", value = "用户标识", required = true, dataType = "Long", paramType="path")
    })
    @GetMapping("/accounts/{id}")
    public RestResponse<AccountVO> getAccount(@RequestParam("jsonToken") String jsonToken,@PathVariable("id") Long id) {
        log.warn(EncryptUtil.decodeUTF8StringBase64(jsonToken));
        AccountDTO dto = accountService.getAccount(id);
        return RestResponse.success(AccountCovert.INSTANCE.dto2vo(dto));

    }



}
